A Procedure for Building Complex Expressions

Description

Filter expressions are character strings. However, it is often very difficult to compose a filter expression. Handling quotes and variable names becomes confusing. The following procedure, written by Cal Locklin, makes the process easier and more understandable. The desired result of a filter expression - like when you want to pass a filter to a report - is a character string. The result that is passed to the report must be exactly the same as if you typed it into the report filter itself. This is an important concept because many people forget that the report has no idea what the variables were in the script that called the report unless those variables were dimensioned as GLOBAL. So a filter like this...

DIM first_date as D
first_date_c = ui_get_date()
first_date = ctod( first_date )
filter = "between( order_date, first_date, date())"
report.preview( "Order_list", filter )

will not work because the report has no idea what first_date refers to. In fact, the report would look for a FIELD named "first_date". For the purpose of this example, we will pass the date using the CTOD("01/01/2002") function to demonstrate the use of quotes in the expression. We would normally enclose the date value in quotes, BUT, since the whole string is enclosed in quotes, how will Alpha Anywhere know which quote is to be used for what? The easiest solution is that any quotes which will be a part of the final text string sent to the report (or wherever it will ultimately be used) must be a single quote. This way Alpha Anywhere can determine which quotes are which and properly construct the final string to be sent. The way I like to start out - especially with complex filters - is to construct the filter just as though I were typing it into the report filter:

between( order_date, ctod("01/01/2002"), date())

Now change all the quotes to single quotes:

between( order_date, ctod('01/01/2002'), date())

Now take out the variable 'stuff' and put double quotes around what's left:

"between( order_date, ctod('"                 "'), date())"

Now, fill the gap with the value you need - including the + signs to concatenate it all:

"between( order_date, ctod('" + first_date_c + "'), date())"

Now just add filter = and you're done:

filter = "between( order_date, ctod('" + first_date_c + "'), date())"

Thanks To:

Cal Locklin

See Also